package lol.clann.minecraft.springboot.api.model.reflect;

import com.ruoshui.utils.java.exception.ExceptionUtils;
import com.ruoshui.utils.java.unsafe.UnsafeUtils;
import lol.clann.minecraft.springboot.api.common.Invoker;
import lombok.Getter;

/**
 * @author: zyp
 * @date: 2020/7/3 3:19 下午
 */
public class UnsafeConstructorInvoker implements Invoker {
    @Getter
    private final Class clazz;

    public UnsafeConstructorInvoker(Class clazz) {
        this.clazz = clazz;
    }

    public Object newInstance() {
        try {
            return UnsafeUtils.allocateInstance(clazz);
        } catch (InstantiationException e) {
            ExceptionUtils.throwException(e);
            return null;
        }
    }

    @Override
    public Object invoke(Object target, Object... params) {
        return newInstance();
    }
}
